package com.atom.common.activiti;

import java.io.Serializable;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.atom.common.dao.DiagramUrlDao;
import com.atom.common.dao.LogUrlDao;
import com.atom.common.entity.DiagramUrlDO;
import com.atom.common.entity.LogUrlDO;
import com.atom.common.util.CommonHtmlGenerateUtil;

/**
 * Title: ProcessCompleteHandler
 * Description: 流程结束时触发，生成流程图静态HTML和操作日志静态HTML
 * @author WEWE
 * @date 2016年6月29日 下午2:27:11
 * @version 1.0
 */
@Component
public class ProcessCompleteHandler implements Serializable {
  
  private static Logger logger = Logger.getLogger(ProcessCompleteHandler.class);
  
  /** serialVersionUID*/
  private static final long serialVersionUID = 1L;
  
  @Resource DiagramUrlDao diagramUrlDao;
  @Resource LogUrlDao logUrlDao;
  
  public void complete(Map<String, Object> var_log, Map<String, Object> var_diagram, String tmpl_log, String tmpl_diagram, String mainProcInsId) {
    // 生成流程图HTML
    saveDiagram(var_diagram, tmpl_diagram, mainProcInsId);
    // 生成操作日志HTML
    saveLog(var_log, tmpl_log, mainProcInsId);    
  }
  
  private void saveDiagram(Map<String, Object> var_diagram, String tmpl_diagram, String mainProcInsId) {
    try {
      String htmlUrl = CommonHtmlGenerateUtil.generate(var_diagram, tmpl_diagram, mainProcInsId);
      DiagramUrlDO entity = new DiagramUrlDO();
      entity.setMainProcessInstanceId(mainProcInsId);
      entity.setHtmlUrl(htmlUrl);
      diagramUrlDao.save(entity);
    } catch (Exception e) {
      logger.error("error message = {}" + e.getMessage(), e);
    }
  }
  
  private void saveLog(Map<String, Object> var_log, String tmpl_log, String mainProcInsId) {
    try {
      String htmlUrl = CommonHtmlGenerateUtil.generate(var_log, tmpl_log, mainProcInsId);
      LogUrlDO entity = new LogUrlDO();
      entity.setMainProcessInstanceId(mainProcInsId);
      entity.setHtmlUrl(htmlUrl);
      logUrlDao.save(entity);
    } catch (Exception e) {
      logger.error("error message = {}" + e.getMessage(), e);
    }
  }

}
